﻿using Farming.Domain;
using Microsoft.EntityFrameworkCore;

namespace Farming.EntityFrameworkCore
{
    public class FarmingDbContext : DbContext
    {
        public FarmingDbContext(DbContextOptions options) : base(options)
        {
        }

        public DbSet<Admin> Admins { get; set; }

        //Fluent Api
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Admin>(m => {
                m.Property(x => x.UserName).HasMaxLength(50).IsRequired();
                m.Property(x => x.Password).HasMaxLength(50).IsRequired();
                m.Property(x => x.LastLoginIp).HasMaxLength(50);
            });

            //数据播种
            modelBuilder.Entity<Admin>().HasData(
                new Admin {
                    UserName = "Admin",
                    Password = Guid.NewGuid().ToString("n"),
                    AdminId = 1,
                    CreateById = 1,
                    CreateByName = "Admin",
                    CreateTime = DateTime.Now,
                    IsDeleted = false
                },
                new Admin
                {
                    UserName = "zhangsan",
                    Password = Guid.NewGuid().ToString("n"),
                    AdminId = 2,
                    CreateById = 1,
                    CreateByName = "Admin",
                    CreateTime = DateTime.Now.AddDays(6),
                    IsDeleted = false
                });


            base.OnModelCreating(modelBuilder);
        }
    }
}
